import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import NaireHomeView from '../views/Naire/NaireHomeView.vue'
import ExamHomeView from '../views/Exam/ExamHomeView.vue'
import UserHomeView from "@/views/User/UserHomeView.vue";
import TrainHomeView from "@/views/Train/TrainHomeView.vue";


const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'workBench',
        component: () => import('../views/WorkBench.vue')
    },
    {
        path: '/naire',
        name: 'naire',
        redirect: '/naireList',
        component: NaireHomeView,
        children: [{
            name: 'naireList',
            path: '/naireList',
            component: () => import('../views/Naire/NaireListView.vue'),
            meta: {
                title: '全部问卷',
                metaType: 'naire'
            }
        },
        {
            name: 'naireQtListView',
            path: '/naireQtListView',
            component: () => import('../views/Naire/NaireQtListView.vue'),
            meta: {
                title: '问卷题库',
                metaType: 'naire'
            }
        }
            ,
        {
            name: 'naireEditView',
            path: '/naireEditView',
            component: () => import('../views/Naire/NaireEditView.vue'),
            meta: {
                title: '问卷编辑',
                metaType: ''
            }
        }
            ,
        {
            name: 'naireItemUserView',
            path: '/naireItemUserView',
            component: () => import('../views/Naire/NaireItemUserView.vue'),
            meta: {
                title: '问卷用户列表',
                metaType: ''
            }
        }
        ]
    },
    {
        path: '/exam',
        name: 'exam',
        redirect: '/examList',
        component: ExamHomeView,
        children: [{
            name: 'examList',
            path: '/examList',
            component: () => import('../views/Exam/ExamListView.vue')
        },
        {
            name: 'examQtListView',
            path: '/examQtListView',
            component: () => import('../views/Exam/ExamQtListView.vue')
        }
            ,
        {
            name: 'examEditView',
            path: '/examEditView',
            component: () => import('../views/Exam/ExamEditView.vue')
        }
            ,
        {
            name: 'examItemUserView',
            path: '/examItemUserView',
            component: () => import('../views/Exam/ExamItemUserView.vue')
        }
        ]
    },
    {
        path: '/user',
        name: 'user',
        redirect: '/userListView',
        component: UserHomeView,
        children: [
            {
                name: 'userListView',
                path: '/userListView',
                component: () => import('../views/User/UserListView.vue')
            },
            {
                name: 'userPreview',
                path: '/userPreview',
                component: () => import('../views/User/UserPreview.vue')
            },
            {
                name: 'dictView',
                path: '/dictView',
                component: () => import('../views/User/DictView.vue')
            },
            {
                name: 'scheduleListView',
                path: '/scheduleListView',
                component: () => import('../views/Schedule/ScheduleListView.vue'),
                children: [

                ]

            },
            {
                name: 'scheduleBaseInfoEditView',
                path: '/scheduleBaseInfoEditView',
                component: () => import('../views/Schedule/ScheduleBaseInfoEditView.vue'),
                children: [

                ]

            },
            {
                name: 'scheduleEditUserView',
                path: '/scheduleEditUserView',
                component: () => import('../views/Schedule/ScheduleEditUserView.vue'),
                children: [

                ]

            }

        ]
    },
    {
        path: '/train',
        name: 'train',
        redirect: '/trainListView',
        component: TrainHomeView,
        children: [
            {
                name: 'trainListView',
                path: '/trainListView',
                component: () => import('../views/Train/TrainListView.vue')
            },
            {
                name: 'trainUserListView',
                path: '/trainUserListView',
                component: () => import('../views/Train/TrainUserListView.vue')
            },
            {
                name: 'addTrainView',
                path: '/addTrainView',
                component: () => import('../views/Train/AddTrainView.vue')
            },
            {
                name: 'dataBankListView',
                path: '/dataBankListView',
                component: () => import('../views/DataBank/DataBankListView.vue')
            },
            {
                name: 'addDataBankView',
                path: '/addDataBankView',
                component: () => import('../views/DataBank/AddDataBankView.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/LoginView.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//  创建一个守卫，用于检查cookie中的token
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token');
    console.log('to', to);
    if (token === null || token === '') {
        if (to.path == '/login') {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
});

export default router
